<template>
    <div class="box-padding" ref="echartsWrapper">
        <Row :gutter="16">
            <Col span="2" style="font-size:14px;color:#697786;font-weight: bold;">辨识评估</Col>
            <Col span="4" style="font-size:14px;color:#697786;">总体辨识：{{mapObj.originRiskTotal}}</Col>
            <Col span="4" offset="10" v-show="personType == 1">
                <Select v-model="formData.enterpriseId" ref="treeSelect" placeholder="公司名称">
                    <Option v-for="item in list1" :value="item.enterpriseId" :key="item.enterpriseId" style="display: none;">
                        {{ item.title }}
                    </Option>
                    <Tree :data="orgList" ref="tree" style="padding:0 10px" @on-select-change="checkEng"></Tree>
                </Select>
            </Col>
            <Col span="4" :offset="personType != 1 ? 14 : 0">
                <Select v-model="formData.projectTeamId" :clearable="(proList.length>0 && personType !=3) || (proList.length>1 && personType ==3)" placeholder="全部项目" @on-change="destroyStatisticsAnalysis()">
                    <Option v-for="item in proList" :value="item.id" :key="item.id">
                        {{ item.shortName }}
                    </Option>
                </Select>
            </Col>
        </Row>
        <firstLine :mapObj="mapObj" ref="first"></firstLine>
        <secondLine ref="second"></secondLine>
        <thirdLine :mapObj="mapObj" ref="third"></thirdLine>
        <fourthLine ref="fourth"></fourthLine>
        <fifthLine></fifthLine>
    </div>
</template>
<script>
import firstLine from './analysis/risk-first-line'
import secondLine from './analysis/risk-second-line'
import thirdLine from './analysis/risk-third-line'
import fourthLine from './analysis/risk-fourth-line'
import fifthLine from './analysis/risk-fifth-line'
export default {
    name:'statistics-risk-detail',
    components:{
        firstLine,
        secondLine,
        thirdLine,
        fourthLine,
        fifthLine,
    },
    data(){
        return{
            model1:'',
            cityList:[],
            formData:{
                enterpriseId:'',
                projectTeamId:'',
            },
            list1:[],
            proList: [], //项目部树
            orgList:[],
            userDetail:JSON.parse(localStorage.getItem('userList')),
            personType:JSON.parse(localStorage.getItem('personType')),
            mapObj:{}
        }
    },
    mounted(){
        //1集团 2工程公司 3项目部
        if (this.personType === 3) {
            this.getProTree({objectId:this.userDetail.projectTeamId})
            if(this.userDetail.userInOrgType == 'PROJECT_TEAM_USER'){
                this.formData.enterpriseId = this.userDetail.projectTeamId
            }else{
                this.formData.enterpriseId = this.userDetail.enterpriseId
            }
        }else if(this.personType === 1){
            this.getOrgTree();
        }else if(this.personType === 2){
            this.getProTree({objectId:this.userDetail.enterpriseId})
            this.formData.enterpriseId = this.userDetail.enterpriseId
        }
    },
    methods:{
        destroyStatisticsAnalysis(){
            this.riskStatisticsStatisticsNormal()
            this.riskStatisticsStatisticsTrend()
        },
        //机构树
        getOrgTree() {
            this.orgList = [];
            this.list1 = [];
            this.$api.measurePlan.getOrgTree().then((res) => {
            if (res.code == 200) {
                this.orgList = res.data
                const tree = this.$refs.tree
                this.$nextTick(() =>  tree.handleSelect(tree.data[0].nodeKey))
            } else {
                this.$Message.error(res.data.msg);
            }
            })
        },
        checkEng(data) {//点击机构树
            if (data.length > 0) {
                this.formData.projectTeamId = '';
                this.list1 = []
                let title = data[0].title
                if(data[0].objectId){
                    let enterpriseId = data[0].objectId
                    this.list1.push({
                        title,
                        enterpriseId
                    })
                    this.getProTree(data[0]);
                    this.formData.enterpriseId = enterpriseId
                    this.$refs.treeSelect.visible = false
                }else {
                    this.$Message.warning('请选择'+title+'下的子集')
                }
            }
        },
        //获取项目树
        getProTree(data) {
            this.proList = [];
            this.$api.measurePlan.getProList(data.objectId).then(res => {
            if (res.code === 200) {
                if(res.data.length>0){
                    this.proList = res.data;
                    if(this.proList.length == 1 && this.personType === 3){
                        this.formData.projectTeamId = this.proList[0].id;
                    }
                    this.riskStatisticsStatisticsNormal()
                    this.riskStatisticsStatisticsTrend()
                }
            } else {
                this.$Message.error(res.message)
            }
            })
        },
        riskStatisticsStatisticsNormal(){
            if(this.formData.projectTeamId == undefined || this.formData.projectTeamId == null){
                this.formData.projectTeamId = 0
            }
            this.$api.statisticsAnalysis.riskStatisticsStatisticsNormal(this.formData).then(resp=>{
                if(resp.code == 200){
                    this.mapObj = resp.data
                    this.$refs.first.drawLine1(resp.data.originRiskBarChartLevel[0])
                    this.$refs.first.drawLine2(resp.data.originRiskBarChartCategory)
                    this.$refs.first.pieChart(resp.data.originRiskPieChartType)
                    this.$refs.third.pieChart(resp.data.dynamicRiskPieChartLevel[0])
                    this.$refs.third.pieChart1(resp.data.dynamicRiskPieChartType)
                    this.$refs.third.pieChart2(resp.data.dynamicRiskPieChartCategory)
                }
            })
        },
        riskStatisticsStatisticsTrend(){
            if(this.formData.projectTeamId == undefined || this.formData.projectTeamId == null){
                this.formData.projectTeamId = 0
            }
            this.$api.statisticsAnalysis.riskStatisticsStatisticsTrend(this.formData).then(resp=>{
                if(resp.code == 200){
                    this.$refs.second.drawLine(resp.data.originRiskLineChartTrend)
                    this.$refs.fourth.drawLine(resp.data.dynamicRiskLineChartTrend)
                }
            })
        }
    }
}
</script>